eststo clear


if("`c(os)'"=="Windows"){
	local tableSaveDir = "ReviewResponseResults\"
	local plotSaveDir = "ReviewResponseResults\"
	local pythonSaveDir = "PythonScripts\"
}
else{
	local tableSaveDir = "ReviewResponseResults/"
	local plotSaveDir = "ReviewResponseResults/"
	local pythonSaveDir = "PythonScripts/"
}

//Establish Control Variable Sets
//Spec1 controls are the same for both registrations and VMT 
local spec1Controls = "c.normYear#i.stateGroup c.quadYear#i.stateGroup asinhMetroPop asinhNonMetroPop asinhMetroInc asinhNonMetroInc logrealmeangasprice logemployment loglicenseddrivers logrealstategdp logroadmileage i.emissionind"

//Other controls are not the same for both registrations and VMT 
//Registration Controls
local spec2Controls = "`spec1Controls' i.transactionDataDummy"
local spec3Controls = "`spec2Controls' i.regDummyExtSpec3"
local spec4Controls = "`spec2Controls' i.regDummyExtSpec4"

//Absorb Vars are always time and state fixed effects
local absorbVars = "ib51.stateGroup ib1995.year"

//Establish Cluster Variable
local clusterVar = "stateGroup year"

//Establish primary null
local primaryNull = 1.6
local altNull = 1
local altNull2 = 0.3
local altNull3 = 0
local altNull4 = -2.5

frame create emissions
frame change emissions

if("`c(os)'"=="Windows"){
	local dataFile = "CleanedData\EmissionsProgramsV2.xlsx"
}
else{
	local dataFile = "CleanedData/EmissionsProgramsV2.xlsx"
}

import excel using "`dataFile'", first sheet("Clean") case(l)

frame change default
frlink m:1 state, frame(emissions)
frget stateemissionsprogram startemission endemission, from(emissions)

gen emissionind = 0
replace emissionind = 1 if year>=startemission & year<=endemission & stateemissionsprogram==1

frame drop emissions

//non-normalized models
local treatmentVar = "1.nosafetyind"


local fsVarsSpec2 = "logregistrations i.nosafetyind `spec2RegControls' i.emissionind"
local rfVarsSpec2 = "logvmt i.nosafetyind `spec2VMTControls' i.emissionind"


//Estimate Models
//Estimate Spec. 1 DiD Model
regFWL logregistrations, nosafetyind, `spec1Controls', `absorbVars', `clusterVar', spec1ResEmissions
eststo spec1
local df = e(N_clust)-1
addRobustFStat "mean:nosafetyind_res"
addTreat "mean:nosafetyind_res"
estadd local dsc = "None" 
estadd local groupFE = "Y"
estadd local timeFE = "Y"
estadd local timeTrend = "Y"
estadd local quadTrend = "Y"
estadd local commonControls = "Y"
estadd local emissionsControls = "Y"
estadd local modelNum = "(1)"

//Estimate Spec. 2 DiD Model 
di "absorb og: `absorbVars'"
regFWL logregistrations, nosafetyind, `spec2Controls', `absorbVars', `clusterVar', spec2ResEmissions
eststo spec2
local df = e(N_clust)-1
addRobustFStat "mean:nosafetyind_res"
addTreat "mean:nosafetyind_res"

estadd local dsc = "Documented" 
estadd local groupFE = "Y"
estadd local timeFE = "Y"
estadd local timeTrend = "Y"
estadd local quadTrend = "Y"
estadd local commonControls = "Y"
estadd local emissionsControls = "Y"
estadd local modelNum = "(2)"

//Estimate Spec. 3 DiD Model
regFWL logregistrations, nosafetyind, `spec3Controls', `absorbVars', `clusterVar', spec3ResEmissions
eststo spec3
local df = e(N_clust)-1
addRobustFStat "mean:nosafetyind_res"
addTreat "mean:nosafetyind_res"
estadd local dsc = "\makecell{Large \nextline Undocumented}" 
estadd local groupFE = "Y"
estadd local timeFE = "Y"
estadd local timeTrend = "Y"
estadd local quadTrend = "Y"
estadd local commonControls = "Y"
estadd local emissionsControls = "Y"
estadd local modelNum = "(3)"

//Estimate Spec. 4 Model
regFWL logregistrations, nosafetyind, `spec4Controls', `absorbVars', `clusterVar', spec4ResEmissions
eststo spec4
local df = e(N_clust)-1
addRobustFStat "mean:nosafetyind_res"
addTreat "mean:nosafetyind_res"
estadd local dsc = "\makecell{Small \nextline Undocumented}" 
estadd local groupFE = "Y"
estadd local timeFE = "Y"
estadd local timeTrend = "Y"
estadd local quadTrend = "Y"
estadd local commonControls = "Y"
estadd local emissionsControls = "Y"
estadd local modelNum = "(4)"

//Export Model Results to TeX Files
//Establish Format
local fmtDiD = `"drop(*) nocon label compress starlevels(* 0.1 ** 0.05 *** 0.01) nodepvar nomtitle nonumbers not nostar noline"'


local noteString = "\hline \hline \end{tabular} \vspace{0.2cm} \begin{tablenotes} \begin{spacing}{0} \begin{footnotesize} \item \textbf{Notes:} This table presents difference-in-differences estimates of the impact of the removal of safety inspections on the log of vehicle registrations.  The unit of observation is state-year. The four specifications pertaining to different levels of ``Data Source Controls'' (DSCs) and control specifications are documented in Table \cref{ssec:Spec}. Standard errors, reported in parentheses, are clustered by state. * p\textless 0.1, ** p\textless 0.05, *** p\textless 0.01 \end{footnotesize} \end{spacing} \end{tablenotes} \end{table}"

//Export Table
esttab spec1 spec2 spec3 spec4 using "`tableSaveDir'RegDiDEmissions.tex", `fmtDiD' title("Diff-in-Diff Results Controlling for Emissions Inspections Programs \label{tab:RegDiDEmissions}") stats(modelNum treatb treatse dsc groupFE timeFE timeTrend quadTrend commonControls emissionsControls fStat N r2, labels("\hline \hline \multicolumn{5}{c}{Dep. Var.: Log Number of Vehicle Registrations} \\ " "Removal of" "Safety Inspections" "\makecell[l]{Data Source \nextline Controls}" "State Fixed Effects" "Year Fixed Effects" "\makecell[l]{State-Specific Linear \\ Time Trends}" "\makecell[l]{State-Specific Quadratic \\ Time Trends}" "Common Controls" "\makecell[l]{Emission Inspections \\ Controls}" "Robust F-Stat" "Number of Obs." "R\textsuperscript{2}")fmt(%8.3f %8.3f %8.3f %8.3f  %8.3f  %8.3f  %8.3f  %8.3f %8.3f %8.0fc %8.3f)) replace postfoot("`noteString'")

